package com.training1.core.modules.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.training1.core.common.service.CrudService;
import com.training1.core.common.utils.DateUtils;
import com.training1.core.common.utils.excel.ExcelUtil;
import com.training1.core.entity.IncomeEntity;
import com.training1.core.entity.SystemUserEntity;
import com.training1.core.entity.sumCashEntity;
import com.training1.core.modules.dao.IncomeDao;

@Service
@Transactional
public class IncomeService extends CrudService<IncomeDao,IncomeEntity>{
	
	//时间工具方法,导出表格时使用
	public static String formatDate(Date date) throws Exception{
		 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		 return formatter.format(date);
	}
	@Resource
	private IncomeDao incomedao;
	
	public List<IncomeEntity> countCash(Map<String, Object> param) {
		return incomedao.countCash(param);
	}
	
	public List<sumCashEntity> sumCash() {
		return incomedao.sumCash();
	}
	
	public List<SystemUserEntity> findUserName(){
		return incomedao.findUserName();
	}
	
	//导出表格
	public void export(HttpServletResponse response,Map<String, Object> param) throws Exception {
		List<IncomeEntity> cqList = incomedao.findexportList(param);//根据dao中的内容用方法
		//设置列标题
		List<String> excelHead = new ArrayList<String>();
		excelHead.add("订单编号");
		excelHead.add("会员姓名");
		excelHead.add("会员手机号");
		excelHead.add("开单中心");
		excelHead.add("收入类型");
		excelHead.add("支付方式");
		excelHead.add("支付渠道");
		excelHead.add("发生时间");
		excelHead.add("金额");
		
		//设置内容
		List<List<String>> dataList = new ArrayList<List<String>>();
		for(IncomeEntity cq : cqList){
			List<String> data = new ArrayList<String>();
			data.add(cq.getOrderNumber());
			data.add(cq.getMemberName());
			data.add(cq.getCellphone());
			data.add(cq.getUserName());
			if((cq.getIncomeType())==1){
				data.add("A区资格消费");
			}else if((cq.getIncomeType())==2){
				data.add("B区资格消费");
			}else if((cq.getIncomeType())==3){
				data.add("充值");
			}else if((cq.getIncomeType())==4){
				data.add("pos机购买");
			}else
			{
				data.add(" ");
			}
			if(cq.getPaymentType()==1){
				data.add("线上");
			}else if(cq.getPaymentType()==2){
				data.add("线下");
			}else
			{
				data.add(" ");
			}
			if(cq.getPaymentChannel()==1){
				data.add("现金支付");
			}else if(cq.getPaymentChannel()==2){
				data.add("银联支付");
			}else if(cq.getPaymentChannel()==3){
				data.add("支付宝支付");
			}else if(cq.getPaymentChannel()==4){
				data.add("微信支付");
			}else if(cq.getPaymentChannel()==5){
				data.add("余额支付");
			}else{
				data.add(" ");
			}
			if(cq.getIncomeTime()!=null){
				data.add(formatDate(cq.getIncomeTime()));
			}else{
				data.add(" ");
			}
			//data.add(cq.getIncomeTime());
			if(cq.getCash()!=0.0d){
				data.add(String.valueOf(cq.getCash()));
			}else{
				data.add(" ");
			}
			//data.add(cq.getCash());
			dataList.add(data);
		}
		
		//设置文件名
		String fileName;
		String sheetName = "sheet1";
		String titleName;
		fileName = "财务纳表_"+DateUtils.getDate().replace("-", "");
		titleName = "财务纳表_"+DateUtils.getDate().replace("-", "");
		
		ExcelUtil.exportExcel(response, excelHead, dataList, fileName, sheetName,titleName);
	}
}
